package hash相关.q387_字符串中的第一个唯一字符;

import java.util.HashMap;

/**
 * Hash o(n)
 */
public class Solution {

    public int firstUniqChar(String s) {
        HashMap<Character, Integer> count = new HashMap<>();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            count.put(c, count.getOrDefault(c, 0) + 1);
        }

        for (int i = 0; i < n; i++) {
            if (count.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }
}






import java.util.*;
class Solution {

    public int firstUniqChar(String s) {
        char[] chars = s.toCharArray();
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        for(int i=0;i<=s.length()-1;i++){
            if(map.containsKey(chars[i])){
                map.put(chars[i],-1);
            }else{
                map.put(chars[i],i);
            }
        }
        for(int i=0;i<=s.length()-1;i++){
           int value = map.get(chars[i]);
            if(value!=-1){
                return value;
            }
            
        }
        return -1; 
    }
}